package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author ZhangXiXi
 * @Description TODO
 * @date 2021/1/9 14:32
 * @Version 1.0
 */
@Slf4j
@DubboService
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Transactional
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemsIds) {
        log.info("[检查组-添加]checkGroup:{},checkitemIds:{}", checkGroup, checkitemsIds);
        //调用dao层
        checkGroupDao.insert(checkGroup);
        //逐条插入关系表数据
        for (Integer checkitemsId : checkitemsIds) {
            checkGroupDao.inserCheckGroupAndCheckItem(checkGroup.getId(), checkitemsId);
        }
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[检查组-分页查询]data：{}", queryPageBean);
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //查询page
        Page<CheckGroup> page = checkGroupDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Transactional
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemsIds) {
        log.info("[检查组-编辑]data：{}", checkGroup, checkitemsIds);
        //1、更新检查组
        checkGroupDao.update(checkGroup);
        //2、更新关联关系
        //2.1、删除原有关系
        checkGroupDao.deleteCheckGroupAndCheckItemByCheckGroupId(checkGroup.getId());
        //2.2、加入当前关联关系
        for (Integer checkitemsId : checkitemsIds) {
            checkGroupDao.inserCheckGroupAndCheckItem(checkGroup.getId(), checkitemsId);
        }
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        log.info("[检查组-根据检查组ID查询关联的检查项ID集和]id:{}", id);

        return checkGroupDao.selectCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public CheckGroup findById(Integer id) {
        log.info("[检查组-根据id查询]id:{}", id);

        return checkGroupDao.selectById(id);
    }

    @Override
    public Result delete(Integer id) {
        log.info("[检查组-根据id删除]id:{}", id);
        List<Integer> list = checkGroupDao.selectCheckItemIdsByCheckGroupId(id);

        if (!list.isEmpty()) {
            checkGroupDao.deleteCheckGroupAndCheckItemByCheckGroupId(id);
        }
        checkGroupDao.deleteById(id);
        return new Result(true, MessageConst.DELETE_CHECKGROUP_SUCCESS);

    }

    @Override
    public List<CheckGroup> findAll() {


        return checkGroupDao.selectAll();
    }
}
